/**   
 * @Title: MenuController.java 
 * @Package com.neusoft.neumooc.controller 
 * @Description: TODO 
 * @author acer   
 * @date 2016年10月27日 下午4:21:39 
 */
package com.neusoft.neumooc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.neusoft.core.util.IDUtil;
import com.neusoft.core.util.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.core.common.BaseController;
import com.neusoft.core.common.conf.Params;
import com.neusoft.neumooc.VO.MenuVO;
import com.neusoft.neumooc.service.MenuService;

/**
 * @ClassName: MenuController
 * @Description: TODO
 * @author Fuyongjun
 * @date 2016年10月27日
 */
@Controller
@RequestMapping(value = "/menu")
public class MenuController extends BaseController {
	@Autowired
	MenuService menuService;

	/**
	 * @Title:getMenuList
	 * @Description: 获取菜单信息
	 * @param pageNo
	 * @param pageSize
	 * @return resMap
	 */
	@ResponseBody
	@RequestMapping(value = "/list")
	public Map<String, Object> getMenuList(HttpServletRequest request,
			HttpServletResponse response, String pageNo, String pageSize)
			throws Exception {
		int pageIndex = (Util.parseInt(pageNo) - 1) * Util.parseInt(pageSize);// 计算分页索引
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			List<MenuVO> menuList = menuService.getMenus(pageIndex,
					Util.parseInt(pageSize));
			int total = menuService.queryMenuCount();// 计算分页数量
			resMap.put("total", total);
			resMap.put("menuList", menuList);
			resMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("获取数据有异常信息抛出:" + e.getMessage(), e.getCause());
			resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
		}
		return resMap;
	}

	/**
	 * @Title:addMenu
	 * @Description: 添加菜单
	 * @param menuvo
	 * @return resMap
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> addMenu(HttpServletRequest request,HttpServletResponse response, MenuVO menuVo) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			boolean flag = menuService.checkRepeat(menuVo.getName());// 检查是否重复
			if (flag) {
				menuVo.setId(IDUtil.getID());
				menuService.addMenu(menuVo);// 添加菜单
				resMap.put("menuVo", menuVo);
				resMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				resMap.put(Params.RET_MSG, "添加菜单成功");
			} else {
				resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				resMap.put(Params.RET_MSG, "添加菜单错误(重复)");
			}
		} catch (Exception e) {
			log.error("添加菜单有异常信息抛出:" + e.getMessage(), e.getCause());
		}
		return resMap;
	}
	/**
	 * @Title:deleteMenu
	 * @Description: 删除菜单
	 * @param id
	 * @return resMap
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> deleteMenu(HttpServletRequest request, String id) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			int count = menuService.deleteMenu(id);// 删除菜单id
			if (count > 0) {
				resMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				resMap.put(Params.RET_MSG, "删除菜单成功");
			} else {
				resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				resMap.put(Params.RET_MSG, "删除菜单失败");
			}
		} catch (Exception e) {
			log.error("删除菜单有异常信息抛出:" + e.getMessage(), e.getCause());
		}
		return resMap;
	}

	/**
	 * @Title:requierMenu
	 * @Description: 获取菜单信息
	 * @param id
	 * @return resMap
	 */
	@RequestMapping(value = "/info")
	@ResponseBody
	public Map<String, Object> menuInfo(HttpServletRequest request, String id) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			MenuVO menuVO = menuService.menuInfo(id);// 获得菜单信息
			resMap.put("menuVO", menuVO);
			resMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
			resMap.put(Params.RET_MSG, "获取菜单信息成功");

		} catch (Exception e) {
			log.error("获取菜单有异常信息抛出:" + e.getMessage(), e.getCause());
			resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			resMap.put(Params.RET_MSG, "获取菜单信息失败");
		}
		return resMap;
	}
	/**
	 * @Title:updateMenu
	 * @Description: 更新菜单
	 * @param menuvo
	 * @return resMap
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Map<String, Object> updateMenu(HttpServletRequest request,
			MenuVO menuVO) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			menuService.updateMenu(menuVO);// 更新菜单
			resMap.put("menuVO", menuVO);
			resMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
			resMap.put(Params.RET_MSG, "更新菜单成功");
		} catch (Exception e) {
			log.error("更新菜单有异常信息抛出:" + e.getMessage(), e.getCause());
			resMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			resMap.put(Params.RET_MSG, "更新菜单失败");
		}
		return resMap;
	}
}